//
//  MODepthDoDetail_IdeaViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/26.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MODepthDoDetail_IdeaViewController.h"
#import "MOWaitDoDetail_IdeaViewCell.h"

@interface MODepthDoDetail_IdeaViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)UIView *headerView;

@property(nonatomic,strong)UIButton *rightButton;

//意见
@property(nonatomic,strong)SNTextView *textView;

@end

@implementation MODepthDoDetail_IdeaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableHeaderView = self.headerView;
    
    [self initData];
}

-(void)initData
{
    self.dataArray = [NSMutableArray array];
    NSArray *titleArr = @[@"审批人：",@"审批人：",@"审批人："];
    for (int i = 0; i<titleArr.count; i++) {
        
        MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
        model.resultName = @"张凯";
        model.duty = @"处长";
        model.depyName = @"技术部";
        model.suggestion = @"已阅";
        model.dateTime = @"2018/07/05";
        model.processMode = @"领导办理";
        model.declare = @"已阅";
        model.openStatus = @"0";
        
        [self.dataArray addObject:model];
        
    }
    [self.tableView reloadData];
    
    
//    NSString *url = [NSString stringWithFormat:@"%@/%@?authorization=%@",ODPSDB_ODPSQUERYSWCLDCLYJ_URL,self.waitModel.key,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
//
//    [PPNetworkHelper GET:url parameters:@{} success:^(id responseObject) {
//        NSLog(@"收文处理单处理意见：%@",responseObject);
//        if ([responseObject[@"status"] integerValue]==1) {
//            NSDictionary *dataDic = responseObject[@"result"];
//            NSArray *arr = dataDic.allKeys;
//            for (int i = 0; i<arr.count; i++) {
//
//                NSString *key = [NSString passNullForString:arr[i]];
//                if (key.length>0) {
//                    NSDictionary *dic = dataDic[arr[i]];
//
//                    MOWaitDoDetail_IdeaModel *model = [[MOWaitDoDetail_IdeaModel alloc]init];
//                    model.resultName = dic[@"name"];
//                    model.duty = dic[@"duty"];
//                    model.depyName = dic[@"depyName"];
//                    model.suggestion = dic[@"suggestion"];
//                    model.dateTime = dic[@"dateTime"];
//                    model.processMode = dic[@"processMode"];
//                    model.declare = dic[@"declare"];
//                    model.openStatus = @"0";
//
//                    [self.dataArray addObject:model];
//                }
//            }
//            [self.tableView reloadData];
//        }
//
//    } failure:^(NSError *error) {
//        NSLog(@"收文处理单处理意见：%@",[error localizedDescription]);
//    }];
    
}

-(void)clickOpenButton:(UIButton *)sender
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[sender.tag];
    if ([model.openStatus integerValue]==1) {
        model.openStatus = @"0";
    }else{
        model.openStatus = @"1";
    }
    [self.tableView reloadData];
}

-(void)clickRightButton
{
    SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    NSArray *arr = @[@"同意",@"不同意"];
    pickview.numberArray = arr;
    
    __weak typeof (self)weakSelf = self;
    pickview.clickIndexBlock = ^(NSInteger index) {
        [weakSelf.rightButton setTitle:arr[index] forState:UIControlStateNormal];
        [weakSelf.rightButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"waitDoDetail_Idea_MyIdea" object:nil userInfo:@{@"text":arr[index]}];
        
    };
    [WINDOW addSubview:pickview];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[indexPath.row];
    
    MOWaitDoDetail_IdeaViewCell *cell = [MOWaitDoDetail_IdeaViewCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.model = model;
    
    cell.rightButton.tag = indexPath.row;
    [cell.rightButton addTarget:self action:@selector(clickOpenButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_IdeaModel *model = self.dataArray[indexPath.row];
    return [MOWaitDoDetail_IdeaViewCell heightForMOWaitDoDetail_IdeaViewCellAndModel:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


-(void)textViewDidEndEditing:(UITextView *)textView
{
    //审批说明
    [[NSNotificationCenter defaultCenter]postNotificationName:@"waitDoDetail_Idea_desc" object:nil userInfo:@{@"text":textView.text}];
}

#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - 50 - 41) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(191))];
        _headerView.backgroundColor = KGrayColor_ededed;
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kHConversions(191)-10)];
        backView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:backView];
        
        UIButton *nameButton = [[UIButton alloc]initWithFrame:CGRectMake(17, 0, 100, kHConversions(54))];
        [nameButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
        nameButton.titleLabel.font = FONT_SIZE(14);
        [nameButton setTitle:@"我的意见:" forState:UIControlStateNormal];
        [nameButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
        [backView addSubview:nameButton];
        
        self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-90, 0, 100, kHConversions(54))];
        self.rightButton.titleLabel.font = FONT_SIZE(15);
        [self.rightButton setTitle:@"请选择" forState:UIControlStateNormal];
        [self.rightButton setImage:IMAGE(@"right_notice") forState:UIControlStateNormal];
        [self.rightButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        [self.rightButton setTitleColor:KGrayColor_BABEC2 forState:UIControlStateNormal];
        [self.rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:self.rightButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.rightButton.bottom, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = KGrayColor_ededed;
        [backView addSubview:lineView];
        
        UIButton *descLabel = [[UIButton alloc]initWithFrame:CGRectMake(17, lineView.bottom, 100, kHConversions(54))];
        [descLabel setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
        descLabel.titleLabel.font = FONT_SIZE(14);
        [descLabel setTitle:@"审批说明:" forState:UIControlStateNormal];
        [descLabel setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
        [backView addSubview:descLabel];
        
        self.textView = [[SNTextView alloc]initWithFrame:CGRectMake(kWConversions(13), descLabel.bottom,(int)(SCREEN_WIDTH - kWConversions(13)*2), (int)(backView.height - 1 - kHConversions(54)*2))];
        self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.textView.backgroundColor = [UIColor whiteColor];
        self.textView.placeholder = @"请输入审批内容";
        self.textView.placeholderColor = KGrayColor_BABEC2;
        self.textView.font = FONT_SIZE(12);
        self.textView.delegate = self;
        self.textView.textColor = KBlackColor_2A2A2A;
        [backView addSubview:self.textView];
        
    }
    return _headerView;
}





@end
